# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
import json
from urllib.parse import urlencode


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "短期水陆公共交通意外保险"

    # 生成产品信息
    def genProductInfo(self):
        plan = {
            "planA": {
                "days": ["7d", "15d", "30d", "365d"],
                "people_count": ["1"],
                "train_death": ["500000"],
                "subway_death": ["500000"],
                "bus_death": ["500000"],
                "taxi_death": ["500000"],
                "ship_death": ["500000"],
            },
            "planB": {
                "days": ["7d", "15d", "30d", "365d"],
                "people_count": ["1"],
                "train_death": ["1000000"],
                "subway_death": ["1000000"],
                "bus_death": ["1000000"],
                "taxi_death": ["1000000"],
                "ship_death": ["1000000"],
            },
            "planC": {
                "days": ["7d", "15d", "30d", "365d"],
                "people_count": ["1"],
                "train_death": ["1500000"],
                "subway_death": ["1500000"],
                "bus_death": ["1500000"],
                "taxi_death": ["1500000"],
                "ship_death": ["1500000"],
            },
            "planD": {
                "days": ["7d", "15d", "30d", "365d"],
                "people_count": ["1"],
                "train_death": ["2000000"],
                "subway_death": ["2000000"],
                "bus_death": ["2000000"],
                "taxi_death": ["2000000"],
                "ship_death": ["2000000"],
            },

        }
        for k in plan:
            for v in product(plan[k]["days"], plan[k]["people_count"], plan[k]["train_death"], plan[k]["subway_death"],
                             plan[k]["bus_death"], plan[k]["taxi_death"], plan[k]["ship_death"]):
                data = Helper.formatProductInfo(plan[k], v)
                yield data

    # 执行爬虫
    def requestJson(self):
        for v in self.genProductInfo():
            params = '''
                appName: 
                appSex: M
                appidType: A
                appidNo: 
                appBirthday: 2020-01-01
                appPhone: 
                appPro: 110000
                appCity: 110100
                appAddress: 
                appEmail: 
                name: 
                sex: M
                idType: A
                idNo: 
                birthday: 2020-01-01
                phone: 
                pro: 110000
                city: 110100
                address: 
                email: 
                source: CUS000000046
                planCode: P000000972
                planAmnt: 500000
                polTerm: 30
                poltermType: D
                payPeriod: 0
                payIntv: 0
                relation: 0
            '''
            data = Helper.perfectJson(params)
            data["planAmnt"] = v["train_death"]
            data["polTerm"] = v["days"].replace("d", "")
            payload = urlencode(data).replace("+", "")
            r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
            if r.status_code == 200 and r.json()["success"]:
                price = Helper.decimalParse(r.json()["msg"])
                data = {
                    "name": self.name,
                    "company": "大都会人寿",
                    "price": price
                }
                data.update(v)
                Helper.mergeTable(self.name, self.mysql, data)
